package com.zbcn.utils.option;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Supplier;

/**
 *  @title Builder
 *  @Description 建造器模式
 *  @author zbcn8
 *  @Date 2020/3/5 13:43
 */
public class Builder<T> {

	private final Supplier<T> supplier;

	private List<Consumer<T>> modifiers = new ArrayList<>();

	public Builder(Supplier<T> supplier) {
		this.supplier = supplier;
	}

	public static <T> Builder<T> of(Supplier<T> supplier){
		return new Builder<>(supplier);
	}

	public <P1> Builder<T> with(Consumer1<T,P1> consumer,P1 p1){
		Consumer<T> c = instance -> consumer.accept(instance,p1);
		modifiers.add(c);
		return this;
	}

	public <P1, P2> Builder<T> with(Consumer2<T, P1, P2> consumer, P1 p1, P2 p2) {
		Consumer<T> c = instance -> consumer.accept(instance, p1, p2);
		modifiers.add(c);
		return this;
	}

	public <P1, P2, P3> Builder<T> with(Consumer3<T, P1, P2, P3> consumer, P1 p1, P2 p2, P3 p3) {
		Consumer<T> c = instance -> consumer.accept(instance, p1, p2, p3);
		modifiers.add(c);
		return this;
	}

	public T build(){
		T value = supplier.get();
		modifiers.forEach(modifier -> modifier.accept(value));
		modifiers.clear();
		return value;
	}

	/**
	 * 参数consumer
	 * @param <T>
	 * @param <P1>
	 */
	@FunctionalInterface
	public interface Consumer1<T,P1>{
		void accept(T t, P1 p1);
	}

	/**
	 * 参数consumer2
	 * @param <T>
	 * @param <P1>
	 * @param <P2>
	 */
	@FunctionalInterface
	public interface Consumer2<T,P1,P2>{
		void accept(T t, P1 p1,P2 p2);
	}


	/**
	 * 3 参数 Consumer
	 */
	@FunctionalInterface
	public interface Consumer3<T, P1, P2, P3> {
		void accept(T t, P1 p1, P2 p2, P3 p3);
	}



}
